package com.topxgun.gcode.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.topxgun.gcode.entity.AttributeEntity;
import com.topxgun.gcode.service.CodeUtils;
import com.topxgun.gcode.utils.ConfigUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/code")
@Controller
public class CodeGenController {
    /**
     * 定义全部模板路径
     */
    public static  final  String TEMP_PATH = ConfigUtils.getPropertiesByKey("flt.template.path");

    @RequestMapping("/genercode")
    @ResponseBody
    public JSONObject genercode(HttpServletRequest request){
        JSONObject result = new JSONObject();
        String codePath = request.getParameter("codePath");
        String packageName = request.getParameter("packageName");
        String className = request.getParameter("className");
        String attributes = request.getParameter("attributes");
        JSONArray attArray = JSONArray.parseArray(attributes);
        List<AttributeEntity> listEntitys = new ArrayList<>();
        for(Object item : attArray){
            JSONObject param = (JSONObject) item;
            String oneAtt = param.getString("attName");
            String[] oneAttAndTypes = oneAtt.split(":");
            AttributeEntity attItem = new AttributeEntity();
            attItem.setAttr(oneAttAndTypes[0]);
            attItem.setCaseAttr(toUpperCaseFirstOne(oneAttAndTypes[0]));
            attItem.setType(oneAttAndTypes[1]);
            listEntitys.add(attItem);
        }
        Map<String,Object> outParam = new HashMap<>();
        outParam.put("packageName",packageName);
        outParam.put("className",className);
        outParam.put("table",toLowerCaseFirstOne(className));
        outParam.put("attrs",listEntitys);
        CodeUtils.entityCode(outParam,TEMP_PATH+"/EntityTemplate.ftl",className,codePath,packageName);
        result = CodeUtils.daoCode(outParam,TEMP_PATH+"/DaoTemplate.ftl",className,codePath,packageName);
        CodeUtils.daoImpCode(outParam,TEMP_PATH+"/DaoImplTemplate.ftl",className,codePath,packageName);
        CodeUtils.serviceCode(outParam,TEMP_PATH+"/ServiceTemplate.ftl",className,codePath,packageName);
        CodeUtils.serviceImplCode(outParam,TEMP_PATH+"/ServiceImplTemplate.ftl",className,codePath,packageName);
        return result;
    }

    /**
     * 将首字母转大写方法
     * @param s
     * @return
     */
    public static String toUpperCaseFirstOne(String s){
        if(Character.isUpperCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
    }

    /**
     * 将首字母转小写方法
     * @param s
     * @return
     */
    public static String toLowerCaseFirstOne(String s){
        if(Character.isLowerCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
    }
}
